/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_enter_reimbursement.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.tb_cost.entity.TbCost;
import com.thinkgem.jeesite.modules.tb_cost.service.TbCostService;
import com.thinkgem.jeesite.modules.tb_driver.entity.TbDriver;
import com.thinkgem.jeesite.modules.tb_driver.service.TbDriverService;
import com.thinkgem.jeesite.modules.tb_enter_reimbursement.entity.TbEnterReimbursement;
import com.thinkgem.jeesite.modules.tb_enter_reimbursement.service.TbEnterReimbursementService;
import com.thinkgem.jeesite.modules.tb_fuel_consumption.entity.TbFuelConsumption;
import com.thinkgem.jeesite.modules.tb_fuel_consumption.service.TbFuelConsumptionService;
import com.thinkgem.jeesite.modules.tb_out_car_register.service.TbOutCarRegisterService;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybill;
import com.thinkgem.jeesite.modules.tb_waybill.service.TbWaybillService;

/**
 * 回车报账Controller
 * @author ZhaoHai
 * @version 2017-07-10
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_enter_reimbursement/tbEnterReimbursement")
public class TbEnterReimbursementController extends BaseController {

	@Autowired
	private TbEnterReimbursementService tbEnterReimbursementService;
	@Autowired
	private TbCostService tbCostService; //出车费用
	@Autowired
	private TbFuelConsumptionService tbFuelConsumptionService; //油料消耗
	@Autowired
	private TbWaybillService tbWaybillService; //运单
	@Autowired
	private TbOutCarRegisterService tbOutCarRegisterService; //出车登记
	@Autowired
	private TbDriverService tbDriverService;//司机
	
	@ModelAttribute
	public TbEnterReimbursement get(@RequestParam(required=false) String id) {
		TbEnterReimbursement entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbEnterReimbursementService.get(id);
		}
		if (entity == null){
			entity = new TbEnterReimbursement();
		}
		return entity;
	}
	
	@RequiresPermissions("tb_enter_reimbursement:tbEnterReimbursement:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbEnterReimbursement tbEnterReimbursement, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbEnterReimbursement> page = tbEnterReimbursementService.findPage(new Page<TbEnterReimbursement>(request, response), tbEnterReimbursement); 
		model.addAttribute("page", page);
		return "modules/tb_enter_reimbursement/tbenterreimbursementshow";
	}

	@RequiresPermissions("tb_enter_reimbursement:tbEnterReimbursement:view")
	@RequestMapping(value = "form")
	public String form(TbEnterReimbursement tbEnterReimbursement, Model model) {
		model.addAttribute("tbEnterReimbursement", tbEnterReimbursement);
		return "modules/tb_enter_reimbursement/tbEnterReimbursementForm";
	}

	@RequiresPermissions("tb_enter_reimbursement:tbEnterReimbursement:edit")
	@RequestMapping(value = "save")
	public String save(TbEnterReimbursement tbEnterReimbursement, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbEnterReimbursement)){
			return form(tbEnterReimbursement, model);
		}
		tbEnterReimbursementService.save(tbEnterReimbursement);
		addMessage(redirectAttributes, "保存回车报账成功");
		return "redirect:"+Global.getAdminPath()+"/tb_enter_reimbursement/tbEnterReimbursement/?repage";
	}
	
	@RequiresPermissions("tb_enter_reimbursement:tbEnterReimbursement:edit")
	@RequestMapping(value = "delete")
	public String delete(TbEnterReimbursement tbEnterReimbursement, RedirectAttributes redirectAttributes) {
		tbEnterReimbursementService.delete(tbEnterReimbursement);
		addMessage(redirectAttributes, "删除回车报账成功");
		return "redirect:"+Global.getAdminPath()+"/tb_enter_reimbursement/tbEnterReimbursement/?repage";
	}
	
	/**
	 * 运输中等待报账的订单
	 */
	@RequiresPermissions("tb_enter_reimbursement:tbEnterReimbursement:view")
	@RequestMapping("insertdata")
	public String insertData(Model model){
		List<TbWaybill>  waybilllist=tbWaybillService.OnTheWayWaybill();
		model.addAttribute("waybilllist", waybilllist);
		return "modules/tb_enter_reimbursement/tbEnterReimbursementList";
	}
	/**
	 * 以id更改回车报账运单的状态 
	 * @param String id
	 * @param String statu
	 */
	@RequestMapping(value = "changeStatu")
	@ResponseBody
	public Map<String,String> changeStatu(@RequestParam("id") String id,@RequestParam("updatastatu") String state,@RequestParam("outCarmoney") Double outCarmoney){
		if(id != null && id != "" && state != null && state != ""){
			tbWaybillService.updateNew(id,state);
			TbWaybill tbWaybill=new TbWaybill();
			tbWaybill.setId(id);
		}
			List<String> waybillId = new ArrayList<String>();
			waybillId.add(id);
			Map<String,String> costmap = new HashMap<String,String>();
			Double accessoryfee=0.00;//配件费
			Double maintenance_cost=0.00;//维修费
			Double upkeep_cost=0.00;//保养费
			Double concert_cost=0.00;//协调费
			Double getaccommodation=0.00;//住宿费
			Double roadtoll=0.00;//过路费
			Double other_cost=0.00;//其他费用
			Double fuel_price=0.00;//加油站现金加油费用
			Double oilcardrefueling=0.00;//油卡
			Double fuel_weight=0.00;//罐车加油质量
			List<TbFuelConsumption> FuelConsumptionlist=tbFuelConsumptionService.reviewedFuel(waybillId);
			    for (TbFuelConsumption tbFuelConsumption : FuelConsumptionlist) {		    	
			    	String type = tbFuelConsumption.getType();
					switch (type) {
					case ("0"):
						fuel_weight+=tbFuelConsumption.getWeight();
						break;
					case ("1"):
						fuel_price+=tbFuelConsumption.getMoney();
						break;
					default:
						break;
					}	
				}
			List<TbCost> costlist=tbCostService.reviewedCost(waybillId);
			for (TbCost tbCost : costlist) {
				String key = tbCost.getType();
				switch (key) {
				case ("0"):
					accessoryfee+=tbCost.getMoney();
					break;
				case ("1"):
					maintenance_cost+=tbCost.getMoney();
					break;
                case ("2"):
                	roadtoll+=tbCost.getMoney();
					break;
				case ("3"):
					concert_cost+=tbCost.getMoney();
					break;
                case ("4"):
                	getaccommodation+=tbCost.getMoney();
					break;
				case ("5"):
					upkeep_cost+=tbCost.getMoney();
					break;
                case ("6"):
                	other_cost+=tbCost.getMoney();
					break;
				default:
					break;
				}								
			}
			Double return_cost=outCarmoney-(accessoryfee+maintenance_cost+roadtoll+concert_cost+getaccommodation+upkeep_cost+other_cost+fuel_price);//应返回费用			
			costmap.put("waybailId",id);
			costmap.put("state", state);
			costmap.put("accessoryfee", accessoryfee.toString());
			costmap.put("oilcardrefueling", oilcardrefueling.toString());
			costmap.put("maintenance_cost", maintenance_cost.toString());
			costmap.put("roadtoll", roadtoll.toString());
			costmap.put("concert_cost", concert_cost.toString());
			costmap.put("getaccommodation", getaccommodation.toString());
			costmap.put("upkeep_cost", upkeep_cost.toString());
			costmap.put("other_cost", other_cost.toString());
			costmap.put("outCarmoney", outCarmoney.toString());			
			costmap.put("fuel_price", fuel_price.toString());
			costmap.put("fuel_weight", fuel_weight.toString());
			costmap.put("return_cost", return_cost.toString());
			return costmap;
	}
	/**
	 * 添加回车报账审核后的信息并且重新载入页面，按钮状态发生改变 
	 * @param HttpServletRequest
	 * @param HttpServletResponse
	 * @param TbEnterReimbursement
	 */
	@RequiresPermissions("tb_enter_reimbursement:tbEnterReimbursement:view")
	@RequestMapping(value = "savedata")
	public String savedata(HttpServletRequest request,HttpServletResponse response,TbEnterReimbursement tbEnterReimbursement){
		    TbEnterReimbursement tbEnterReimbursement1=tbEnterReimbursementService.showbywaybailId(request.getParameter("waybailId"));
		    if(tbEnterReimbursement1 == null){
		    	tbEnterReimbursement.setWaybailId(request.getParameter("waybailId"));
				tbEnterReimbursement.setCashRefueling(request.getParameter("fuel_price"));
				tbEnterReimbursement.setOilCardRefueling(request.getParameter("oilcardrefueling"));//油卡
				tbEnterReimbursement.setRefuelingTanker(request.getParameter("fuel_weight"));
				tbEnterReimbursement.setToll(request.getParameter("roadtoll"));
				tbEnterReimbursement.setAccessoriesFee(request.getParameter("accessoryfee"));
				tbEnterReimbursement.setMaintenance(request.getParameter("maintenance_cost"));
				tbEnterReimbursement.setMaintenanceFee(request.getParameter("upkeep_cost"));
				tbEnterReimbursement.setXieTiaoFei(request.getParameter("concert_cost"));
				tbEnterReimbursement.setQuarterage(request.getParameter("getaccommodation"));
				tbEnterReimbursement.setRest(request.getParameter("other_cost"));
				tbEnterReimbursement.setTheCarDrawMoney(request.getParameter("outCarmoney"));
				tbEnterReimbursement.setRepayment(request.getParameter("return_cost"));			
				tbEnterReimbursementService.save(tbEnterReimbursement);
				tbWaybillService.updateNew(request.getParameter("waybailId"),"6");
				return "modules/tb_enter_reimbursement/tbEnterReimbursementList";
		    }else{
		    	return null;
		    }
		    
	}
	
	/**
	 * 查看已报账的信息
	 */
	@RequestMapping(value = "showtbEnterReimbursement")
	@ResponseBody
	public Map<String,String> showtbEnterReimbursement(TbEnterReimbursement tbEnterReimbursement,@RequestParam("id") String waybailId){
		tbEnterReimbursement=tbEnterReimbursementService.showbywaybailId(waybailId);
		Map<String,String> Entermap=new HashMap<String,String>();		
			Entermap.put("waybailId", tbEnterReimbursement.getWaybailId());
			Entermap.put("cashRefueling",tbEnterReimbursement.getCashRefueling());
			Entermap.put("oilCardRefueling",tbEnterReimbursement.getOilCardRefueling());
			Entermap.put("refuelingTanker",tbEnterReimbursement.getRefuelingTanker());
			Entermap.put("toll",tbEnterReimbursement.getToll());
			Entermap.put("accessoriesFee",tbEnterReimbursement.getAccessoriesFee());
			Entermap.put("maintenance",tbEnterReimbursement.getMaintenance());
			Entermap.put("maintenanceFee",tbEnterReimbursement.getMaintenanceFee());
			Entermap.put("xieTiaoFei",tbEnterReimbursement.getXieTiaoFei());
			Entermap.put("quarterage",tbEnterReimbursement.getQuarterage());
			Entermap.put("rest",tbEnterReimbursement.getRest());
			Entermap.put("carDrawMoney",tbEnterReimbursement.getTheCarDrawMoney());
			Entermap.put("repayment",tbEnterReimbursement.getRepayment());
		return Entermap;
	}
}